package com.pengweiwei.androidzygote.functions.httprequest;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.pengweiwei.androidzygote.R;
import com.pengweiwei.androidzygote.main.UrlCenter;
import com.putong35.common.baseactfrag.activity.BaseActivity;
import com.putong35.common.httprequest.IHttpRequest;
import com.putong35.common.httprequest.UrlParams;
import com.putong35.common.httprequest.app.MyHttpRequest;
import com.putong35.common.httprequest.responsecallbacks.ListBeanResponseListener;
import com.putong35.common.httprequest.responsecallbacks.SingleBeanResponseListener;
import com.putong35.common.httprequest.responsecallbacks.StringResponseListener;
import com.putong35.common.utils.EasyToast;
import com.putong35.common.utils.SLog;

import java.util.List;

import butterknife.OnClick;

public class HttpDemoIndexActivity extends BaseActivity {

    public static final void start(Context fromContext) {
        Intent intent = new Intent(fromContext, HttpDemoIndexActivity.class);
        fromContext.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_http_demo_index, null);
    }

    @OnClick({R.id.btnReqSingleData, R.id.btnReqSingleDataFailure, R.id.btnReqSingleDataFailureNoData, R.id.btnReqListData, R.id.btnReqListDataEmpty, R.id.btnReqListDataSpecial, R.id.btnReqOriginalData})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btnReqSingleData:
                reqSingleData("normal_data");
                break;
            case R.id.btnReqSingleDataFailure:
                reqSingleData("normal_data_failure");
                break;
            case R.id.btnReqSingleDataFailureNoData:
                reqSingleData("normal_data_failure_nodata");
                break;
            case R.id.btnReqListData:
                reqListData("list_data");
                break;
            case R.id.btnReqListDataEmpty:
                reqListData("list_data_empty");
                break;
            case R.id.btnReqListDataSpecial:
                reqListDataSpecial("list_data_spectial");
                break;
            case R.id.btnReqOriginalData:
                reqOriginalData("list_data_spectial");
                break;
        }
    }

    private void reqOriginalData(String fileName) {
        IHttpRequest request = new MyHttpRequest(this);
        UrlParams params = new UrlParams();
        params.put("file_name", fileName);
        request.get(UrlCenter.URL_NORMAL_DATA, params, new StringResponseListener() {
            @Override
            public void onSuccess(String o) {
                EasyToast.showToast(HttpDemoIndexActivity.this, "onSuccess " + o);
                SLog.e("onSuccess " + o);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                EasyToast.showToast(HttpDemoIndexActivity.this, "onFailure " + strMsg);
                SLog.e("onFailure " + strMsg);
            }

        });
    }

    private void reqSingleData(String fileName) {
        IHttpRequest request = new MyHttpRequest(this);
        UrlParams params = new UrlParams();
        params.put("file_name", fileName);
        request.get(UrlCenter.URL_NORMAL_DATA, params, new SingleBeanResponseListener<UserBean>(UserBean.class) {
            @Override
            public void onSuccess(UserBean userBean) {
                EasyToast.showToast(HttpDemoIndexActivity.this, userBean.toString());
                SLog.e(userBean.toString());
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                String logStr = "onFailure: msg " + strMsg + "  code = " + errorNo;
                SLog.e(logStr);
                EasyToast.showToast(HttpDemoIndexActivity.this, logStr);
            }
        });
    }

    private void reqListData(String fileName) {
        IHttpRequest request = new MyHttpRequest(this);
        UrlParams params = new UrlParams();
        params.put("file_name", fileName);
        request.get(UrlCenter.URL_NORMAL_DATA, params, new ListBeanResponseListener<UserBean>(UserBean.class) {
            public void onSuccess(List<UserBean> userList) {
                EasyToast.showToast(HttpDemoIndexActivity.this, userList.toString());
                SLog.e(userList.toString());
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                String logStr = "onFailure: msg " + strMsg + "  code = " + errorNo;
                SLog.e(logStr);
                EasyToast.showToast(HttpDemoIndexActivity.this, logStr);
            }
        });
    }


    private void reqListDataSpecial(String fileName) {
        IHttpRequest request = new MyHttpRequest(this);
        UrlParams params = new UrlParams();
        params.put("file_name", fileName);
        request.get(UrlCenter.URL_NORMAL_DATA, params, new ListBeanResponseListener<String>(String.class) {
            public void onSuccess(List<String> userList) {
                EasyToast.showToast(HttpDemoIndexActivity.this, userList.toString());
                SLog.e(userList.toString());
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                String logStr = "onFailure: msg " + strMsg + "  code = " + errorNo;
                SLog.e(logStr);
                EasyToast.showToast(HttpDemoIndexActivity.this, logStr);
            }
        });
    }
}
